﻿/*
 * 公共库-标准库
 * 标准库功能每个项目完全统一，只允许添加公用功能。如需修改请联系负责人。
 * 负责人：周大通
 * 修改日期：2017.02.24
 */
using System.Collections;
using System.Collections.Generic;

using UnityEngine;

/// <summary>
/// Transform操作的工具类。
/// </summary>
public class TransformUtility : MonoBehaviour
{
    public static void SetParent(Transform tran, Transform parent, bool resetTransform = true)
    {
        if (tran.parent != parent)
        {
            tran.SetParent(parent);

            if (resetTransform)
            {
                ResetTransform(tran);
            }
        }
    }

    public static void ResetTransform(Transform tran)
    {
        if (tran is RectTransform)
        {
            RectTransform rectTran = (RectTransform)tran;
            rectTran.anchoredPosition = Vector2.zero;
            rectTran.offsetMax = rectTran.offsetMin = Vector2.zero;
        }
        else
        {
            tran.localPosition = Vector3.zero;
        }

        tran.localRotation = Quaternion.identity;
        tran.localScale = Vector3.one;
    }

    public static void ResetRectTransform(RectTransform rectTran)
    {
        //rectTran.anchoredPosition = Vector2.zero;
        rectTran.anchorMin = Vector2.zero;
        rectTran.anchorMax = Vector2.one;
        rectTran.sizeDelta = Vector2.zero;

        rectTran.localRotation = Quaternion.identity;
        rectTran.localScale = Vector3.one;
    }


    /// <summary>
    /// 计算UI元素所处屏幕空间的比例
    /// </summary>
    /// <returns></returns>
    public static Rect CalculatePercentOfUIInScreenSpace(RectTransform rectTrans, Camera uiCam)
    {
        Rect rect = new Rect();
        Vector2[] screenPoints = GetRectTransformCornersInScreenSpace(rectTrans, uiCam);
        SaturateVector2(screenPoints, new Vector2(0, Screen.width), new Vector2(0, Screen.height));

 
        rect.position = screenPoints[0];
        rect.width = (screenPoints[3].x - screenPoints[0].x);
        rect.height = (screenPoints[1].y - screenPoints[0].y);

        return rect;
    }


    public static Vector2[] GetRectTransformCornersInScreenSpace(RectTransform rectTrans, Camera uiCam)
    {
        if (uiCam == null)
        {
            Debug.LogError("GetRectTransformCornersInScreenSpace uiCam==NULL!");
            return null;
        }

        Vector2[] result = new Vector2[4];
        Vector3[] worldPos = new Vector3[4];
        rectTrans.GetWorldCorners(worldPos);

        for (int i = 0; i < worldPos.Length; i++)
        {
            result[i] = RectTransformUtility.WorldToScreenPoint(uiCam, worldPos[i]);
        }
        return result;
    }
    public static void SaturateVector2(Vector2[] vectors, Vector2 xBounds, Vector2 yBounds)
    {
        for (int i = 0; i < vectors.Length; i++)
        {
            SaturateVector2(ref vectors[i], xBounds, yBounds);
        }

    }
    public static void SaturateVector2(ref Vector2 vector, Vector2 xBounds, Vector2 yBounds)
    {
        vector.x = Mathf.Clamp(vector.x, xBounds.x, xBounds.y);
        vector.y = Mathf.Clamp(vector.y, yBounds.x, yBounds.y);
    }
}
